//
//  ConstraintHomePageUtil.m
//  宜居2
//

#import "ConstraintUtil.h"
#import "ContainerView.h"
#import "Masonry.h"
#define kTotalHeight [UIScreen mainScreen].bounds.size.height


@implementation ConstraintUtil
{
    UIScrollView *_superView;
}

- (void)setupConstraints:(UIScrollView *)scrollView{
    if (self.totalHeight <= 0) {
        self.totalHeight = kTotalHeight;
    }
    _superView = scrollView;
    ContainerView *lastView = nil;
    if (self.views != nil && self.views.count > 0 && self.totalHeight > 0) {
        for (int i = 0; i < self.views.count; i++) {
            ContainerView *view = self.views[i];
            [scrollView addSubview:view];
            CGFloat height = self.totalHeight * view.ratio ;
            [view makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(scrollView);
                make.width.equalTo(scrollView);
                if (!view.isAutoSize) {
                    make.height.equalTo(height);
                }else{
                    make.height.mas_greaterThanOrEqualTo(self.totalHeight*0.2);
                }
                if (lastView) {
                    make.top.equalTo(lastView.bottom);
                }else  {
                    make.top.equalTo(scrollView);
                }
            }];
            lastView = view;
        }
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(scrollView.superview);
            // 让scrollview的contentSize随着内容的增多而变化
            make.bottom.equalTo(lastView.mas_bottom);
        }];
    }
}

- (void)setupUpdateConstraints{
    
    ContainerView *lastView = nil;
    
    if (self.views != nil && self.views.count > 0 && self.totalHeight > 0) {
        
        for (int i = 0; i < self.views.count; i++) {
            
            ContainerView *view = self.views[i];
            
            CGFloat height = self.totalHeight * view.ratio;
            
            [view updateConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(_superView);
                
                make.width.equalTo(_superView);
                
                if (!view.isAutoSize) {
                    make.height.equalTo(height);
                }else{
                    make.height.mas_greaterThanOrEqualTo(self.totalHeight*0.2);
                }
    
                if (lastView) {
                    make.top.equalTo(lastView.bottom);
                    
                }else {
                    make.top.equalTo(_superView);
                }
            }];
            lastView = view;
        }
        
        [_superView updateConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(_superView.superview);
            
            // 让scrollview的contentSize随着内容的增多而变化
            
            make.bottom.equalTo(lastView.mas_bottom);
        }];
        
    }
}

-(void)handleScaleEventResponseWithRatio:(CGFloat)ratio
                        andContainerView:(UIView *)containerView{
    
    for (ContainerView *view in self.views) {
        
        if ([view isEqual:containerView]) {
            
            view.ratio = ratio;
        }
    }
    
    [self setupUpdateConstraints];
    
    [UIView animateWithDuration:1 animations:^{
        
        [_superView layoutIfNeeded];
    }];
}


@end
